﻿using System;

namespace RexToy.UI.Controls
{
    public class CalendarButton : Button
    {
        public CalendarButton(string text)
            : base(text)
        {
            __dragEventBubble = false;
            this.__domNode.GetStyle().Margin = "0px";
            this.Standalone = false;
            this.Palette = Palette.C;
        }
    }

    public class Calendar : InPlacePopup
    {
        private uint __year = 2000;
        public uint Year
        {
            get { return __year; }
            set
            {
                this.__year = value;
                this.Refresh();
            }
        }
        private uint __month = 1;
        public uint Month
        {
            get { return __month; }
            set
            {
                this.__month = value;
                this.Refresh();
            }
        }
        private uint __date = 1;

        public uint Date
        {
            get { return __date; }
            set { __date = value; }
        }

        private Table table;
        private CalendarButton yearBtn;
        private CalendarButton monthBtn;

        public string[] events = new string[] { "onselect" };

        public Calendar()
        {
            this.Palette = Palette.B;

            var d = new DateTime();
            this.__year = d.FullYear;
            this.__month = d.Month + 1;

            this.table = RexToy.UI.Controls.Table.Create(7, 7);

            var style = this.table.DomNode.GetStyle();
            style.FontSize = "75%";

            this.Initialize(this.table);

            this.Refresh();
            this.AppendControl(this.table);
        }

        private void Initialize(Table table)
        {
            var arr = new string[] { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" };

            for (uint i = 0; i < 7; i++)
            {
                var btn = new RexToy.UI.Controls.CalendarButton(arr[i]);
                btn.Palette = Palette.B;
                table.GetCell(0, i).AppendControl(btn);
            }

            for (uint i = 1; i < 7; i++)
            {
                for (uint j = 0; j < 7; j++)
                {
                    var btnDate = new RexToy.UI.Controls.CalendarButton("&nbsp;");
                    table.GetCell(i, j).AppendControl(btnDate);
                    btnDate.AttachEvent("onclick", new Action<object>(this.ClickDate).Bind(this));
                }
            }

            TableCell c;
            var row = table.InsertRow(0);
            var prevYearBtn = new RexToy.UI.Controls.CalendarButton("<");
            c = row.InsertCell(0);
            c.AppendControl(prevYearBtn);
            this.yearBtn = new RexToy.UI.Controls.CalendarButton(this.__year.ToString());
            this.yearBtn.Palette = Palette.D;
            c = row.InsertCell(1);
            c.ColSpan = 2;
            c.AppendControl(this.yearBtn);
            var nextYearBtn = new RexToy.UI.Controls.CalendarButton(">");
            c = row.InsertCell(2);
            c.AppendControl(nextYearBtn);

            var prevMonthBtn = new RexToy.UI.Controls.CalendarButton("<");
            c = row.InsertCell(3);
            c.AppendControl(prevMonthBtn);
            this.monthBtn = new RexToy.UI.Controls.CalendarButton(this.__month.ToString());
            this.monthBtn.Palette = Palette.D;
            c = row.InsertCell(4);
            c.AppendControl(this.monthBtn);
            var nextMonthBtn = new RexToy.UI.Controls.CalendarButton(">");
            c = row.InsertCell(5);
            c.AppendControl(nextMonthBtn);

            prevYearBtn.AttachEvent("onclick", new Action(this.ClickPrevYear).Bind(this));
            nextYearBtn.AttachEvent("onclick", new Action(this.ClickNextYear).Bind(this));
            prevMonthBtn.AttachEvent("onclick", new Action(this.ClickPrevMonth).Bind(this));
            nextMonthBtn.AttachEvent("onclick", new Action(this.ClickNextMonth).Bind(this));
        }

        private void ClickNextYear()
        {
            this.__year++;
            this.yearBtn.Text = this.__year.ToString();
            this.Refresh();
        }

        private void ClickPrevYear()
        {
            this.__year--;
            this.yearBtn.Text = this.__year.ToString();
            this.Refresh();
        }

        private void ClickNextMonth()
        {
            this.__month++;
            if (this.__month == 13)
                this.__month = 1;
            this.monthBtn.Text = this.__month.ToString();
            this.Refresh();
        }

        private void ClickPrevMonth()
        {
            this.__month--;
            if (this.__month == 0)
                this.__month = 12;
            this.monthBtn.Text = this.__month.ToString();
            this.Refresh();
        }

        private void ClickDate(object sender)
        {
            var btn = sender as CalendarButton;
            this.Close();
            this.__date = (uint)System.Global.ParseInt(btn.Text);
            this.__FireEvent("onselect");
        }

        private void Refresh()
        {
            this.yearBtn.Text = this.__year.ToString();
            this.monthBtn.Text = this.__month.ToString();

            var first = new DateTime(this.__year, this.__month - 1, 1, 0, 0, 0, 0);
            var prevLast = new DateTime();
            prevLast.Time = first.Time - 24 * 3600 * 1000;
            var nextFirst = new DateTime(this.__year, this.__month, 1, 0, 0, 0, 0);
            var length = Math.Floor((nextFirst.Time - first.Time) / 1000 / 24 / 3600);

            var col = first.Day;
            uint row = 2;
            for (uint i = 0; i < col; i++)
            {
                var day = prevLast.Date - col + i + 1;
                var btn = this.table.GetCell(row, i).Controls[0] as CalendarButton;
                btn.Text = day.ToString();
                btn.Enable = false;
            }

            for (uint day = 1; day < length + 1; day++)
            {
                var cell = this.table.GetCell(row, col);
                var btn = this.table.GetCell(row, col).Controls[0] as CalendarButton;
                btn.Text = day.ToString();
                btn.Enable = true;
                col++;
                if (col == 7)
                {
                    col = 0;
                    row++;
                }
            }

            for (var day = 1; day < 14; day++)
            {
                if (row == 8)
                    break;
                var btn = this.table.GetCell(row, col).Controls[0] as CalendarButton;
                btn.Text = day.ToString();
                btn.Enable = false;
                col++;
                if (col == 7)
                {
                    col = 0;
                    row++;
                }
            }
        }
    }
}
